package com.mp.goods.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mp.common.BaseUncheckException;
import com.mp.common.Message;
import com.mp.goods.bean.Standard;
import com.mp.goods.dto.InsertStandardDTO;
import com.mp.goods.dto.SelectStandardDTO;
import com.mp.goods.dto.UpdateStandardDTO;
import com.mp.goods.mapper.StandardMapper;
import com.mp.goods.service.StandardService;
import com.mp.goods.vo.StandardVO;
import com.mp.goods.vo.StandardVO;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 规格表 服务实现类
 * </p>
 *
 * @author notejava
 * @since 2020-07-10
 */
@Service
@Transactional(rollbackFor=Exception.class)
public class StandardServiceImpl extends ServiceImpl<StandardMapper, Standard> implements StandardService {

    //baseMapper就是我们的 StandardMapper的实例
    @Override
    public Page<StandardVO> selectPage(SelectStandardDTO model) {
        //条件构造器
        //QueryWrapper<Standard> wrapper = new QueryWrapper<>();
        //wrapper.like("standardName",model.getStandardName());
        //where standard_name like '%'#{standarName}'%'
        //wrapper.lambda().like(!StringUtils.isEmpty(model.getStandardName()),Standard::getStandardName,model.getStandardName());
        Standard standard = new Standard();
        BeanUtil.copyProperties(model,standard);
        return baseMapper.page(model,standard);
    }

    @Override
    public StandardVO insert(InsertStandardDTO insertStandardDTO) {
        Standard standard = new Standard();
        StandardVO standardVO = new StandardVO();
        BeanUtil.copyProperties(insertStandardDTO,standard);
        if("张三".equals(insertStandardDTO.getStandardName())){
            throw BaseUncheckException.fail("名称不能为张三");
        }
        super.save(standard);
        BeanUtil.copyProperties(standard,standardVO);
        return standardVO;
    }

    @Override
    public Boolean delete(Long standardId) {
        return super.removeById(standardId);
    }


    @Override
    public StandardVO updateById(UpdateStandardDTO updateStandardDTO) {
        Standard standard = baseMapper.selectById(updateStandardDTO.getId());
        StandardVO standardVO = new StandardVO();
        if(standard!=null){
            //属性复制
           BeanUtil.copyProperties(updateStandardDTO,standard);
           super.updateById(standard);
            //将结果以vo返回
            BeanUtil.copyProperties(standard,standardVO);
        }else{
          throw BaseUncheckException.fail("更新失败");
        }
        return standardVO;
    }
}
